#include "global.h"

GLDEF_D FILE_DATA				file = { NULL } ;
GLDEF_D GLOB					glob = { FALSE, FALSE } ;
GLDEF_D GFX						gfx ;
GLDEF_D METRICS				metrics ;

GLDEF_C VOID Quit(VOID) {
	glob.quit = TRUE ;
}

GLDEF_C VOID MainDialog(VOID) {
	glob.main = TRUE ;
}

/* Globally accessible routines to manipulate the main output window */

/* Print str at line in the main window */
GLDEF_C VOID PrintString(TEXT *str, INT line) {
	P_RECT			rect ;
	G_GC				gc ;

	rect.tl.x = 0 ;
	rect.tl.y = line * (metrics.text_height + TEXT_LEADING) ;
	rect.br.x = gfx.wdata.extent.width ;
	rect.br.y = rect.tl.y + metrics.text_height + TEXT_LEADING ;

	gc.style = gfx.style ;
	gCreateTempGC(gfx.win_id, G_GC_MASK_STYLE, &gc) ;
	gPrintBoxText(	&rect,
						metrics.text_ascent,
						G_TEXT_ALIGN_LEFT,
						0,
						str,
						p_slen(str) ) ;
	gFreeTempGC() ;
}

GLDEF_C BOOL MoveMainWindow(INT key_dir) {
	BOOL		mv = FALSE ;

	if ( !gfx.super )
		return(FALSE) ;

	switch (key_dir) {

		case W_KEY_END :
		case W_KEY_RIGHT :
			mv = gfx.wdata.extent.width + gfx.wdata.extent.tl.x > metrics.disp_size.x ;
			if ( mv ) {
				if ( key_dir == W_KEY_RIGHT )
					gfx.wdata.extent.tl.x -= 10 ;
				else
					gfx.wdata.extent.tl.x = metrics.disp_size.x - gfx.wdata.extent.width ;

				gfx.wdata.extent.tl.x = MAX(metrics.disp_size.x - gfx.wdata.extent.width, gfx.wdata.extent.tl.x) ;
			}
			break ;

		case W_KEY_HOME :
		case W_KEY_LEFT :
			mv = gfx.wdata.extent.tl.x < 0 ;
			if ( mv ) {
				if ( key_dir == W_KEY_LEFT )
					gfx.wdata.extent.tl.x += 10 ;
				else
					gfx.wdata.extent.tl.x = 0 ;

				gfx.wdata.extent.tl.x = MIN(0, gfx.wdata.extent.tl.x) ;
			}
			break ;

		case W_KEY_UP :
		case W_KEY_PAGE_UP :
			mv = gfx.wdata.extent.tl.y < 0 ;
			if ( mv ) {
				if ( key_dir == W_KEY_UP )
					gfx.wdata.extent.tl.y += 10 ;
				else
					gfx.wdata.extent.tl.y = 0 ;

				gfx.wdata.extent.tl.y = MIN(0, gfx.wdata.extent.tl.y) ;
			}
			break ;

		case W_KEY_DOWN :
		case W_KEY_PAGE_DOWN :
			mv = gfx.wdata.extent.height + gfx.wdata.extent.tl.y > metrics.disp_size.y ;
			if ( mv ) {
				if ( key_dir == W_KEY_DOWN )
					gfx.wdata.extent.tl.y -= 10 ;
				else
					gfx.wdata.extent.tl.y = metrics.disp_size.y - gfx.wdata.extent.height ;

				gfx.wdata.extent.tl.y = MAX(metrics.disp_size.y - gfx.wdata.extent.height, gfx.wdata.extent.tl.y) ;
			}
			break ;
		default :
			mv = FALSE ;
			break ;
	}
	if ( mv )
		wSetWindow(gfx.win_id, W_WIN_EXTENT, &gfx.wdata) ;
	return( mv ) ;
}

GLDEF_C INT GetMainWid(VOID) {
	return(gfx.win_id) ;
}

GLDEF_C VOID SetMainWindowExtent(WORD tl_x, WORD tl_y, WORD width, WORD height) {
	gfx.wdata.extent.tl.x = tl_x ;
	gfx.wdata.extent.tl.y = tl_y ;
	gfx.wdata.extent.width = width ;
	gfx.wdata.extent.height = height ;
	wSetWindow(gfx.win_id, W_WIN_EXTENT, &gfx.wdata) ;
}

GLDEF_C VOID GetMainWindowExtent(P_EXTENT *pextent) {
	wInquireWindow(gfx.win_id, &gfx.wdata) ;
	pextent->tl.x = gfx.wdata.extent.tl.x ;
	pextent->tl.y = gfx.wdata.extent.tl.y ;
	pextent->width = gfx.wdata.extent.width ;
	pextent->height = gfx.wdata.extent.height ;
}

GLDEF_C VOID CopytoMainWindow(UINT bitmap_id, P_RECT *prect) {
	P_POINT			pos = { 0, 0 } ;

	gCreateTempGC0(gfx.win_id) ;
	gCopyBit(&pos, bitmap_id, prect, G_TRMODE_REPL) ;
	gFreeTempGC() ;
}

GLDEF_C VOID UpdateStatus(TEXT *str) {
	UpdateBar(&gfx.status_bar, str) ;
}

GLDEF_C VOID UpdateXScroll(SCROLL_BAR_STATE *pbarstate) {
	UpdateBar(&gfx.scroll_x, pbarstate) ;
}

GLDEF_C VOID UpdateYScroll(SCROLL_BAR_STATE *pbarstate) {
	UpdateBar(&gfx.scroll_y, pbarstate) ;
}
